﻿package birdry.ui		{
	
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	/**
	* ...
	* @author DefaultUser (Tools -> Custom Arguments...)
	*/
	public class  Drawing	{
		
		var _canvas:MovieClip;
		var _drawLayer:MovieClip;
		var _borderSize:uint = 5;
		var _borderColor:uint = 0x000000;
		var _stage:Stage;
		var _canvasW:uint;
		var _canvasH:uint;
		var _maskMC:MovieClip;
		
		function Drawing( canvas:MovieClip ): void	{
			_canvas = canvas;
			_borderSize = borderSize;
			_borderColor = borderColor;
			setLayout();
		}
		
		private function setLayout(): void	{
			_drawLayer = new MovieClip();
			_canvas.addChild(_drawLayer);
			_canvasW = _canvas.width;
			_canvasH = _canvas.height;
		}
		
		private function addEvent(): void	{
			_canvas.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_canvas.addEventListener(MouseEvent.MOUSE_UP, onUp);
			_canvas.stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
		}
		
		private function removeEvent(): void	{
			_canvas.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_canvas.removeEventListener(MouseEvent.MOUSE_UP, onUp);
			_canvas.stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
		}
		
		
		var shape:Shape = new Shape();
		private function onDown( e: MouseEvent ): void	{
			drawStart();
			_canvas.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
		}
		
		private function onUp( e: MouseEvent ): void	{
			_canvas.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
		}
		
		private function onMove( e: MouseEvent ): void	{
			shape.graphics.lineTo(_canvas.mouseX, _canvas.mouseY);
			shape.graphics.endFill();
			_drawLayer.addChild(shape);
		}
		
		private function drawStart(): void	{
			shape = new Shape();
			shape.graphics.beginFill(0x000000);
			shape.graphics.lineStyle(_borderSize, _borderColor);
			shape.graphics.drawCircle(_canvas.mouseX, _canvas.mouseY, _borderSize/10);
		}
		
		
		public function get borderColor(): int	{
			return _borderColor;
		}
		
		public function set borderColor( value: int ): void	{
			_borderColor = value;
		}
		
		public function get borderSize(): int	{
			return _borderSize;
		}
		
		public function set borderSize( value: int ): void	{
			_borderSize = value;
		}
		
		
		public function start(): void	{
			addEvent();
		}
		
		public function stop(): void	{
			removeEvent();
		}
		
		public function setMask(mc:MovieClip): void	{
			_drawLayer.mask = mc;
			_maskMC = mc;
		}
		
		public function clear(): void	{
			_canvas.removeChild(_drawLayer);
			_drawLayer = new MovieClip();
			_canvas.addChild(_drawLayer);
			if (_maskMC)	{
				setMask(_maskMC);
			}
		}
		
	}	// class
	
}	// package